﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.DanhMuc
{
    public partial class frmDonViTinhgf : Form
    {
        public frmDonViTinhgf()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();
        string tag = "";

        private void load_button(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Text = "";
                txtTenDonViTinh.Enabled = true;
                cmbDonViTinhGoc.Enabled = true;
                txtTyLe.Text = "";
                txtTyLe.Enabled = true;
                cmbTrangThai.Enabled = true;
                txtGhiChu.Text = "";
                txtGhiChu.Enabled = true;

            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Enabled = true;
                cmbDonViTinhGoc.Enabled = true;
                txtTyLe.Enabled = true;
                cmbTrangThai.Enabled = true;
                txtGhiChu.Enabled = true;
            }
            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;

                txtMaDonViTinh.Enabled = false;
                txtTenDonViTinh.Enabled = false;
                cmbDonViTinhGoc.Enabled = false;
                txtTyLe.Enabled = false;
                cmbTrangThai.Enabled = false;
                txtGhiChu.Enabled = false;
            }
        }

        private void load_grid()
        {
            if (cn.KetNoi())
            {
                DataTable tbl = cn.getTable("select row_number() over(order by MaDonViTinh) as STT, tbl_DonViTinh.MaDonViTinh, tbl_DonViTinh.TenDonViTinh, tbl_DonViTinh.MaDonViTinhGoc, tbl_DonViTinhGoc.TenDonViTinhGoc, tbl_DonViTinh.TyLe, tbl_DonViTinh.TrangThai,tbl_DonViTinh.GhiChu from tbl_DonViTinh INNER JOIN tbl_DonViTinhGoc ON tbl_DonViTinh.MaDonViTinhGoc = tbl_DonViTinhGoc.MaDonViTinhGoc");
                dtgDonViTinh.DataSource = tbl;
            }
        }

        private void frmDonViTinh_Load(object sender, EventArgs e)
        {
            load_button(tag);

            cn.KetNoi();
            colTrangThai.DataSource = cn.getTable("select TrangThai,"+" nameTT= case TrangThai when 1 then N'Sử dụng'"+" when 0 then N'Không sử dụng' end from tbl_DonViTinh");
            colTrangThai.DisplayMember = "nameTT";
            colTrangThai.ValueMember = "TrangThai";

            cmbDonViTinhGoc.DataSource = cn.getTable("select * from tbl_DonViTinhGoc where TrangThai=1");
            cmbDonViTinhGoc.DisplayMember = "TenDonViTinhGoc";
            cmbDonViTinhGoc.ValueMember = "MaDonViTinhGoc";

            cmbTrangThai.DataSource = cn.CreateTrangThai();
            cmbTrangThai.DisplayMember = "TenTrangThai";
            cmbTrangThai.ValueMember = "TrangThai";

            load_grid();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupPanel4_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_button(tag);
            txtMaDonViTinh.Text = cn.SinhMa("MaDonViTinh","tbl_DonViTinh","DV");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            load_button("");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (this.TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (tag == "them")
            {
                if (txtTenDonViTinh.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("Bạn chưa nhập tên đơn vị tính");
                    txtTenDonViTinh.Focus();
                    return;
                }
                else if (txtTyLe.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("Bạn chưa nhập tỷ lệ quy đổi");
                    txtTyLe.Focus();
                    return;
                }
                string TenDonViTinh = cn.LayDuLieu("select TenDonViTinh from tbl_DonViTinh where lower(TenDonViTinh)=N'"+txtTenDonViTinh.Text.ToLower()+"'");
                if (TenDonViTinh != "")
                {
                    MessageBox.Show("Đơn vị tính đã tồn tại","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    txtTenDonViTinh.Focus();
                    return;
                }
                cn.Insert_Table("tbl_DonViTinh", "MaDonViTinh,TenDonViTinh,MaDonViTinhGoc,TyLe,TrangThai,GhiChu", "'" + txtMaDonViTinh.Text + "',N'" + txtTenDonViTinh.Text + "','" + cmbDonViTinhGoc.SelectedValue.ToString() + "','" + txtTyLe.Text + "','" + cmbTrangThai.SelectedValue.ToString() + "',N'"+txtGhiChu.Text+"'");
            }
            else
            {
                cn.Update_Table("tbl_DonViTinh","TenDonViTinh=N'"+txtTenDonViTinh.Text+"',MaDonViTinhGoc='"+cmbDonViTinhGoc.SelectedValue.ToString()+"',TyLe='"+txtTyLe.Text+"',TrangThai='"+cmbTrangThai.SelectedValue.ToString()+"',GhiChu=N'"+txtGhiChu.Text+"'","MaDonViTinh='"+txtMaDonViTinh.Text+"'");
            }
            load_button("");
            load_grid();
        }


        private void dtgDonViTinh_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgDonViTinh.SelectedRows.Count > 0)
            {
                txtMaDonViTinh.Text = dtgDonViTinh.CurrentRow.Cells["MaDonViTinh"].Value.ToString();
                txtTenDonViTinh.Text = dtgDonViTinh.CurrentRow.Cells["TenDonViTinh"].Value.ToString();
                cmbDonViTinhGoc.SelectedValue = dtgDonViTinh.CurrentRow.Cells["MaDonViTinhGoc"].Value.ToString();
                txtTyLe.Text = dtgDonViTinh.CurrentRow.Cells["TyLe"].Value.ToString();
                cmbTrangThai.SelectedValue = dtgDonViTinh.CurrentRow.Cells["colTrangThai"].Value.ToString();
                txtGhiChu.Text = dtgDonViTinh.CurrentRow.Cells["colGhiChu"].Value.ToString();
            }
        }
    }
}
